SSH Client PRO
The SSHClient class provides an interface for connecting to a remote SSH server, executing commands, opening TTY or PTY sessions, transferring files via SFTP, and performing multi-hop SSH jumps. It supports both command-based and interactive terminal-based workflows.
This class is central to establishing and managing SSH sessions in your script.
Static Methods
SSHClient.connect(options): Promise<SSHClient>
Establishes a connection to a remote SSH server.
Parameters:
-
options(object):-
host(string): The hostname or IP address of the SSH server. -
port?(number): The port number to connect to. Defaults to22. -
authenticationMethod(SSHAuthenticationMethod): The authentication method to use (e.g., password, RSA key). -
trustedHostKeys?(string[]): Optional list of trusted server public keys. If provided, the client will validate the server against this list. -
reconnect?("never" | "once" | "always"): Optional strategy for reconnecting if the connection drops. Default is"never".
-
Returns:
- A
Promisethat resolves to anSSHClientinstance upon successful connection.
Example:
Properties
onDisconnect: (() => void) | null
Callback function to be invoked when the SSH connection is lost or closed.
Example:
Instance Methods
executeCommand(command: string, options?): Promise<string>
Executes a shell command on the remote server and returns its output.
Parameters:
-
command(string): The command to execute. -
options?(object):-
maxResponseSize?(number): Maximum number of bytes to return. -
includeStderr?(boolean): Iftrue, includes standard error output in the result. -
inShell?(boolean): Iftrue, executes the command inside a shell (e.g.,sh -c). Default isfalse.
-
Returns:
- A
Promisethat resolves to the command output as a string.
Example:
executeCommandStream(command, onOutput, options?): Promise<void>
Executes a command and streams its output line-by-line.
Parameters:
-
command(string): The command to run. -
onOutput(function): Callback(data: Data, isStderr: boolean) => booleanCalled for each line of output. Returnfalseto stop receiving output. -
options?:inShell?(boolean): Whether to run the command in a shell.
Returns:
- A
Promisethat resolves when the command completes.
Example:
withPTY(options): Promise<TTYStdinWriter>
Opens a PTY (pseudo-terminal) session.
Parameters:
-
options(object):-
wantReply?(boolean): Whether to wait for a reply from the server. Defaults totrue. -
term?(string): Terminal type (default is"xterm"). -
terminalCharacterWidth?(number): Terminal character width. Default is80. -
terminalRowHeight?(number): Terminal row height. Default is24. -
terminalPixelWidth?(number): Terminal pixel width. Default is0. -
terminalPixelHeight?(number): Terminal pixel height. Default is0. -
onOutput(function): Callback(data: Data, isStderr: boolean) => booleanfor receiving terminal output. -
onError?(function): Optional error callback(error: string) => void.
-
Returns:
- A
Promisethat resolves to aTTYStdinWriterinstance.
Example:
withTTY(options): Promise<TTYStdinWriter>
Opens a TTY session with simplified options (without explicit dimensions).
Parameters:
-
options(object):-
onOutput(function): Callback(data: Data, isStderr: boolean) => booleanfor receiving terminal output. -
onError?(function): Optional error callback(error: string) => void.
-
Returns:
- A
Promisethat resolves to aTTYStdinWriter.
openSFTP(): Promise<SFTPClient>
Opens an SFTP session for file transfer operations.
Returns:
- A
Promisethat resolves to anSFTPClientinstance.
Example:
jump(options): Promise<SSHClient>
Performs an SSH jump (proxy) to another remote host from the current SSH session.
Parameters:
-
options(object):-
host(string): The destination host to jump to. -
port?(number): Port to connect to (default is22). -
authenticationMethod(SSHAuthenticationMethod): Authentication method for the next host. -
trustedHostKeys?(string[]): Optional list of trusted host keys.
-
Returns:
- A
Promisethat resolves to a newSSHClientrepresenting the jump connection.
Example:
close(): Promise<void>
Closes the SSH connection and releases associated resources.
Important: You should call this method when the SSH client is no longer needed to avoid potential memory or socket leaks.
Returns:
- A
Promisethat resolves when the SSH connection is successfully closed.
